package antempire.control.game.operation;

import java.util.HashMap;

import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.StrokeFont;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.graphics.Typeface;
import antempire.control.GameEnum.Ant_Name;

public class TextureRegionCreator {

	private static TextureRegionCreator uniqueInstance;

	private TextureRegionCreator() {
	}

	public static TextureRegionCreator getInstance() {
		if (uniqueInstance == null) {
			uniqueInstance = new TextureRegionCreator();
		}
		return uniqueInstance;
	}

	private TiledTextureRegion creatMovingAntTextureRegion(Ant_Name name,
			BaseGameActivity activity) {
		TiledTextureRegion fishTextureRegion = this.createTiledTextureRegion(
				ModelInformationController.getInstance()
						.getAntInformation(name).get_TextureRegion_width(),
				ModelInformationController.getInstance()
						.getAntInformation(name).get_TextureRegion_height(),
				ModelInformationController.getInstance()
						.getAntInformation(name).get_Path(),
				ModelInformationController.getInstance()
						.getAntInformation(name).get_line(),
				ModelInformationController.getInstance()
						.getAntInformation(name).get_row(), activity);

		return fishTextureRegion;
	}

	public void creatAllMovingAntTextureRegion(
			HashMap<Ant_Name, TiledTextureRegion> allMovingAntTextureRegionMap,
			BaseGameActivity activity) {

		allMovingAntTextureRegionMap
				.put(Ant_Name.MONOMORIUM, this.creatMovingAntTextureRegion(
						Ant_Name.MONOMORIUM, activity));
		allMovingAntTextureRegionMap.put(Ant_Name.PHEIDOLE,
				this.creatMovingAntTextureRegion(Ant_Name.PHEIDOLE, activity));
		allMovingAntTextureRegionMap.put(Ant_Name.LOS,
				this.creatMovingAntTextureRegion(Ant_Name.LOS, activity));
		allMovingAntTextureRegionMap
				.put(Ant_Name.DOLICHODERINAE, this.creatMovingAntTextureRegion(
						Ant_Name.DOLICHODERINAE, activity));

	}

	public TiledTextureRegion creatWaterTextureRegion(BaseGameActivity activity) {
		TiledTextureRegion waterTextureRegion = this.createTiledTextureRegion(
				ModelInformationController.getInstance().getWaterInformation()
						.get_TextureRegion_width(), ModelInformationController
						.getInstance().getWaterInformation()
						.get_TextureRegion_height(), ModelInformationController
						.getInstance().getWaterInformation().get_Path(), 1, 1,
				activity);

		return waterTextureRegion;
	}

	/*
	 * public void creatAllWaterTextureRegion( HashMap<Artillery_Rank,
	 * TiledTextureRegion> allWaterTextureRegionMap, BaseGameActivity activity)
	 * { allWaterTextureRegionMap.put(Artillery_Rank.RANK5,
	 * this.creatWaterTextureRegion(activity)); }
	 */

	/*
	 * public void createAllCaptureWaterTextureRegion( HashMap<Artillery_Rank,
	 * TiledTextureRegion> allNetTextureRegionMap, BaseGameActivity activity) {
	 * allNetTextureRegionMap.put(Artillery_Rank.RANK1,
	 * this.creatNetTextureRegion(Artillery_Rank.RANK1, activity));
	 * allNetTextureRegionMap.put(Artillery_Rank.RANK2,
	 * this.creatNetTextureRegion(Artillery_Rank.RANK2, activity));
	 * allNetTextureRegionMap.put(Artillery_Rank.RANK3,
	 * this.creatNetTextureRegion(Artillery_Rank.RANK3, activity));
	 * allNetTextureRegionMap.put(Artillery_Rank.RANK4,
	 * this.creatNetTextureRegion(Artillery_Rank.RANK4, activity));
	 * allNetTextureRegionMap.put(Artillery_Rank.RANK5,
	 * this.creatNetTextureRegion(Artillery_Rank.RANK5, activity));
	 * 
	 * }
	 */

	public TiledTextureRegion creatCaptureWaterTextureRegion(
			BaseGameActivity activity) {
		TiledTextureRegion captureWaterTextureRegion = this
				.createTiledTextureRegion(
						ModelInformationController.getInstance()
								.getWaterInformation()
								.getCaptureWaterInformation()
								.get_TextureRegion_width(),
						ModelInformationController.getInstance()
								.getWaterInformation()
								.getCaptureWaterInformation()
								.get_TextureRegion_height(),
						ModelInformationController.getInstance()
								.getWaterInformation()
								.getCaptureWaterInformation().get_Path(),

						1, 5, activity);

		return captureWaterTextureRegion;
	}

	public void creatAllCapturedAntTextureRegion(
			HashMap<Ant_Name, TiledTextureRegion> allCapturedAntTextureRegionMap,
			BaseGameActivity activity) {
		allCapturedAntTextureRegionMap.put(Ant_Name.MONOMORIUM, this
				.creatCapturedAntTextureRegion(Ant_Name.MONOMORIUM, activity));
		allCapturedAntTextureRegionMap
				.put(Ant_Name.PHEIDOLE, this.creatCapturedAntTextureRegion(
						Ant_Name.PHEIDOLE, activity));
		allCapturedAntTextureRegionMap.put(Ant_Name.LOS,
				this.creatCapturedAntTextureRegion(Ant_Name.LOS, activity));
		allCapturedAntTextureRegionMap.put(Ant_Name.DOLICHODERINAE, this
				.creatCapturedAntTextureRegion(Ant_Name.DOLICHODERINAE,
						activity));

	}

	private TiledTextureRegion creatCapturedAntTextureRegion(Ant_Name name,
			BaseGameActivity activity) {
		TiledTextureRegion antTextureRegion = this.createTiledTextureRegion(
				ModelInformationController.getInstance()
						.getAntInformation(name).getCaptureAntInformation()
						.get_TextureRegion_width(), ModelInformationController
						.getInstance().getAntInformation(name)
						.getCaptureAntInformation().get_TextureRegion_height(),
				ModelInformationController.getInstance()
						.getAntInformation(name).getCaptureAntInformation()
						.get_Path(), ModelInformationController.getInstance()
						.getAntInformation(name).getCaptureAntInformation()
						.get_line(), ModelInformationController.getInstance()
						.getAntInformation(name).getCaptureAntInformation()
						.get_row(),

				activity);

		return antTextureRegion;
	}

	public void creatAllScoreTextureRegion(
			HashMap<Ant_Name, TiledTextureRegion> allScoreTextureRegionMap,
			BaseGameActivity activity) {
		allScoreTextureRegionMap.put(Ant_Name.MONOMORIUM,
				this.creatScoreTextureRegion(Ant_Name.MONOMORIUM, activity));
		allScoreTextureRegionMap.put(Ant_Name.PHEIDOLE,
				this.creatScoreTextureRegion(Ant_Name.PHEIDOLE, activity));
		allScoreTextureRegionMap.put(Ant_Name.LOS,
				this.creatScoreTextureRegion(Ant_Name.LOS, activity));
		allScoreTextureRegionMap
				.put(Ant_Name.DOLICHODERINAE, this.creatScoreTextureRegion(
						Ant_Name.DOLICHODERINAE, activity));
	}

	private TiledTextureRegion creatScoreTextureRegion(Ant_Name name,
			BaseGameActivity activity) {

		TiledTextureRegion scoreTextureRegion = this.createTiledTextureRegion(
				ModelInformationController.getInstance()
						.getAntInformation(name).getScoreInformation()
						.get_TextureRegion_width(), ModelInformationController
						.getInstance().getAntInformation(name)
						.getScoreInformation().get_TextureRegion_height(),
				ModelInformationController.getInstance()
						.getAntInformation(name).getScoreInformation()
						.get_Path(), ModelInformationController.getInstance()
						.getAntInformation(name).getScoreInformation()
						.get_line(), ModelInformationController.getInstance()
						.getAntInformation(name).getScoreInformation()
						.get_row(), activity);

		return scoreTextureRegion;
	}

	public Font creatFont(BaseGameActivity activity, float pSize, int pColor) {
		Texture fontTexture = new Texture(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		Font font = new Font(fontTexture, Typeface.create(Typeface.DEFAULT,
				Typeface.NORMAL), pSize, true, pColor);
		activity.getEngine().getTextureManager().loadTexture(fontTexture);
		activity.getEngine().getFontManager().loadFonts(font);
		return font;
	}

	public StrokeFont createStrokeFont(BaseGameActivity activity, float pSize,
			int pColor, float pStrokeWidth, int pStrokeColor) {
		Texture fontTexture = new Texture(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		StrokeFont strokeFont = new StrokeFont(fontTexture, Typeface.create(
				Typeface.DEFAULT, Typeface.BOLD), pSize, true, pColor,
				pStrokeWidth, pStrokeColor);
		activity.getEngine().getTextureManager().loadTexture(fontTexture);
		activity.getEngine().getFontManager().loadFonts(strokeFont);
		return strokeFont;
	}

	public TextureRegion createTextureRegion(int pWidth, int pHeight,
			TextureOptions textureOptions, String path,
			BaseGameActivity activity) {
		Texture texture = new Texture(pWidth, pHeight, textureOptions);
		TextureRegion textureRegion = TextureRegionFactory.createFromAsset(
				texture, activity, path, 0, 0);
		activity.getEngine().getTextureManager().loadTexture(texture);
		return textureRegion;
	}

	public TiledTextureRegion createTiledTextureRegion(int pWidth, int pHeight,
			String path, int pTileColumns, int pTileRows,
			BaseGameActivity activity) {
		Texture texture = new Texture(pWidth, pHeight,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TiledTextureRegion tiledTextureRegion = TextureRegionFactory
				.createTiledFromAsset(texture, activity, path, 0, 0,
						pTileColumns, pTileRows);
		activity.getEngine().getTextureManager().loadTexture(texture);
		return tiledTextureRegion;
	}

	public void createAllNumberTextureRegion(
			HashMap<Integer, TextureRegion> allNumberTextureRegionMap,
			BaseGameActivity activity) {
		for (int i = 0; i < 10; i++)
			allNumberTextureRegionMap.put(i, this
					.createTextureRegion(ModelInformationController
							.getInstance().getNumberInformation(i)
							.get_TextureRegion_width(),
							ModelInformationController.getInstance()
									.getNumberInformation(i)
									.get_TextureRegion_height(),
							TextureOptions.BILINEAR_PREMULTIPLYALPHA,
							ModelInformationController.getInstance()
									.getNumberInformation(i).get_Path(),
							activity));
	}

	public TextureRegion createBackgroundTextureRegion(BaseGameActivity activity) {
		String path;
		// switch(mDifficulty){
		// case 1 : path = "background_easy.png"; break;
		// case 2 : path = "background_common.png"; break;
		// case 3 : path = "background_hard.png"; break;
		// default : path = "background_easy.png";
		// }
		//
		// Texture texture = new Texture(1024, 512, TextureOptions.DEFAULT);
		// TextureRegion backgroundTextureRegion =
		// TextureRegionFactory.createFromAsset(texture, activity, path, 0, 0);
		// activity.getEngine().getTextureManager().loadTexture(texture);

		TextureRegion backgroundTextureRegion = this.createTextureRegion(
				ModelInformationController.getInstance().getGameInformation()
						.get_TextureRegion_width(), ModelInformationController
						.getInstance().getGameInformation()
						.get_TextureRegion_height(), TextureOptions.DEFAULT,
				ModelInformationController.getInstance().getGameInformation()
						.get_Path(), activity);

		return backgroundTextureRegion;
	}
	public TextureRegion createBackgroundTreeTextureRegion(BaseGameActivity activity) {
		String path;
		// switch(mDifficulty){
		// case 1 : path = "background_easy.png"; break;
		// case 2 : path = "background_common.png"; break;
		// case 3 : path = "background_hard.png"; break;
		// default : path = "background_easy.png";
		// }
		//
		// Texture texture = new Texture(1024, 512, TextureOptions.DEFAULT);
		// TextureRegion backgroundTextureRegion =
		// TextureRegionFactory.createFromAsset(texture, activity, path, 0, 0);
		// activity.getEngine().getTextureManager().loadTexture(texture);

		TextureRegion backgroundTextureRegion = this.createTextureRegion(
				ModelInformationController.getInstance().getGameBGInformation()
						.get_TextureRegion_width(), ModelInformationController
						.getInstance().getGameBGInformation()
						.get_TextureRegion_height(), TextureOptions.DEFAULT,
				ModelInformationController.getInstance().getGameBGInformation()
						.get_Path(), activity);

		return backgroundTextureRegion;
	}
}
